﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using App_Code.Modules;
using Portal.Core.Binding;
using Telerik.Web.UI;
using Content = Portal.Data.Entity.Content;

public partial class Modules_Content_Edit : ContentModule
{
    public string PublishLink = "#";
    protected override Control Root
    {
        get
        {
            return phContent;
        }
    }
    private Content _currentObj, _editContent;
    protected Content EditContent
    {
        get
        {
            if (_editContent == null)
            {
                object obj = Binder.GetData(_currentObj, phContent, new BindContext());
                if (obj != null)
                {
                    _editContent = (Content)obj;
                }
            }
            return _editContent;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            SetRadEditorPlugin(Body);
        }

        _currentObj = (Content)FindByPrimaryKey();

        Binder.SetData(phContent, _currentObj, new BindContext());
        Body.Content = _currentObj.GetBody();
        PublishLink = _currentObj.URL;
        //binding book of content
        var b = _currentObj.GetBook();
        if (b != null)
        {
            books.Items.Add(new ListItem(string.Format("{1}-[{2},ID:{0},chap:{3},visit:{4},status:{5}]", b.ID, b.FullName, b.CrawlHost, b.TotalChap, b.TotalVisit, b.Status), b.ID.ToString()));
        }
    }

    void SetRadEditorPlugin(RadEditor editor)
    {
        editor.DisableFilter(EditorFilters.ConvertCharactersToEntities | EditorFilters.RemoveScripts);
        editor.EditModes = EditModes.Design | EditModes.Html;
        editor.StripFormattingOptions = EditorStripFormattingOptions.MSWord |
                                                            EditorStripFormattingOptions.MSWordNoFonts |
                                                            EditorStripFormattingOptions.Css |
                                                            EditorStripFormattingOptions.Font |
                                                            EditorStripFormattingOptions.Span |
                                                            EditorStripFormattingOptions.ConvertWordLists;
    }
    protected override void Save_Click(object sender, EventArgs e)
    {
        EditContent.BodyTemp = Body.Content;
        EditContent.Update();
        base.AfterSave(EditContent);

        //cập nhật lại chương cuối nếu đã bị thay đổi (vì có thể chap này là chương cuối nhưng đổi cho chap khác, hoặc nó nhảy đến cuối làm chap cuối)
        if (_currentObj.Chapter != EditContent.Chapter)
        {
            var book = EditContent.GetBook();
            if (book != null)
            {
                var newlastchap = Content.FindLatest(book.ID);
                if (newlastchap != null && book.LastChapId != newlastchap.ID)
                {
                    book.ChangeLastChap(newlastchap.ID);
                }
            }
        }
    }

    protected void OnTryCrawl(object sender, EventArgs e)
    {
        Body.Content = Content.Crawler(CrawlUrl.Text);
    }

    protected void OnNextCurrent(object sender, EventArgs e)
    {
        int idnext = 0;
        Content.FindNextChapUrl(_currentObj.Book_ID, _currentObj.Chapter, out idnext);
        if (idnext > 0)
            Response.Redirect("Admin_Content_content_lists_edit.aspx?id=" + idnext);
    }
}